package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.WorkHandover;
import com.amsp.oa.oademo.mapper.WorkHandoverMapper;
import com.amsp.oa.oademo.service.WorkHandoverService;
import com.amsp.oa.oademo.vo.WorkHandoverVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class WorkHandoverServiceImpl extends ServiceImpl<WorkHandoverMapper, WorkHandover>
        implements WorkHandoverService {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public IPage<WorkHandover> getList(Integer page, Integer size, String name, String jobNumber, String department) {
        LambdaQueryWrapper<WorkHandover> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (StringUtils.hasText(name)) {
            queryWrapper.like(WorkHandover::getName, name);
        }

        if (StringUtils.hasText(jobNumber)) {
            queryWrapper.like(WorkHandover::getJobNumber, jobNumber);
        }

        if (StringUtils.hasText(department)) {
            queryWrapper.like(WorkHandover::getDepartment, department);
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc(WorkHandover::getCreateTime);

        // 分页查询
        Page<WorkHandover> pageParam = new Page<>(page, size);
        return page(pageParam, queryWrapper);
    }

    @Override
    public WorkHandoverVO getDetail(Long id) {
        WorkHandover entity = getById(id);
        if (entity == null) {
            return null;
        }
        return convertToVO(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(WorkHandoverVO vo) {
        WorkHandover entity = convertToEntity(vo);
        return saveOrUpdate(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDelete(List<Long> ids) {
        return removeByIds(ids);
    }

    /**
     * 将实体转换为VO
     *
     * @param entity 实体
     * @return VO
     */
    private WorkHandoverVO convertToVO(WorkHandover entity) {
        if (entity == null) {
            return null;
        }

        WorkHandoverVO vo = new WorkHandoverVO();
        BeanUtils.copyProperties(entity, vo);

        // 格式化时间
        if (entity.getCreateTime() != null) {
            vo.setCreateTime(entity.getCreateTime().format(FORMATTER));
        }

        return vo;
    }

    /**
     * 将VO转换为实体
     *
     * @param vo VO
     * @return 实体
     */
    private WorkHandover convertToEntity(WorkHandoverVO vo) {
        if (vo == null) {
            return null;
        }

        WorkHandover entity = new WorkHandover();
        BeanUtils.copyProperties(vo, entity);

        // 设置状态
        if (entity.getStatus() == null) {
            entity.setStatus("pending");
        }

        return entity;
    }
}